package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 * @author syl
 * @since 2022-06-26
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService apUserService;

    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        // 构建查询条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        //状态
        if (dto.getStatus()!=null) {
            queryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        //分页
        Page<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());
        Page<ApUserRealname> page1 = this.page(page, queryWrapper);
        return new PageResponseResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords());
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int i) {
        //0:驳回   1:通过
        ApUserRealname userRealname = this.getById(dto.getId());
        if(i==0){
            userRealname.setStatus(2);
            userRealname.setReason(dto.getMsg());
            this.updateById(userRealname);
        }else{
            ApUser apUser = apUserService.getById(userRealname.getUserId());
            WmUser wmUser = saveWmUser(apUser);
            ApAuthor apAuthor = saveAuthor(apUser,wmUser);
            wmUser.setApAuthorId(apAuthor.getId());
            mediaFeign.updateWmUser(wmUser);
            userRealname.setStatus(9);
            this.updateById(userRealname);
            apUser.setIsIdentityAuthentication(true);
            apUser.setFlag(1);
            apUserService.updateById(apUser);
        }
        return ResponseResult.okResult();
    }

    @Autowired
    private ArticleFeign articleFeign;

    private ApAuthor saveAuthor(ApUser apUser,WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
        //apAuthor.setId();
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2);
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);
        if (responseResult.getCode()==0) {
            return responseResult.getData();
        }else{
            throw new RuntimeException("远程调用失败");
        }
    }

    @Autowired
    private MediaFeign mediaFeign;

    private WmUser saveWmUser(ApUser apUser){
        WmUser wmUser = new WmUser();
        //wmUser.setId();自增
        wmUser.setApUserId(apUser.getId());
        //wmUser.setApAuthorId();
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
        //wmUser.setNickname();
        wmUser.setImage(apUser.getImage());
        //wmUser.setLocation();
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(9);
        //wmUser.setEmail();
        wmUser.setType(0);
        wmUser.setScore(0);
        wmUser.setLoginTime(new Date());
        ResponseResult<WmUser> responseResult = mediaFeign.saveWmUser(wmUser);
        if (responseResult.getCode()==0) {
            return responseResult.getData();
        }else{
            throw new RuntimeException("远程调用失败");
        }
    }
}
